<?php
class users_model extends CI_Model {
	var $fields = array('username', 'first_name','last_name', 'email', 'address1', 'address2', 'city', 'state', 'phone','mobile','username', 'mypassword', 'active' );
	var $required_fields = array('username','email','first_name', 'last_name');
	//var $table_name = 'users';
	var $table_name = 'admins';
    function users_model()
    {
        parent::__construct();
    }
    
    function getUsers($start = 0, $limit = 10,$where = array('deleted' => 0))
	{
    	$this->db->order_by('id DESC');
    	$this->db->where($where);
    	$query = $this->db->get($this->table_name,$limit,$start);
		$records = $query->result_array();

		$this->db->select('count(id) as total');
    	$this->db->where($where);
    	$rst = $this->db->get($this->table_name);
		$total = $rst->first_row()->total;
		
		$obj->records = $records;
		$obj->total = $total;
		return $obj;
    }
    
    
	function required($data = array()) {
    	if(empty($data)) return false;
    	$this->obj =& get_instance();
    	$status = $this->obj->retrieve->required($this->required_fields, $data);
    	return $status;
    }
    
	function add($data = array()){
    	if(empty($data)) return -1;
    	if(!empty($data['mypassword']))
    		$data['mypassword'] = md5($data['mypassword']);
    	else 
    		$data['mypassword'] = '';
    	if(!$this->checkUniqueUsername($data['username'])) return -3;	
    	if($this->required($data)) {
			$status = $this->db->insert($this->table_name, $data);
			return $status;
    	} else {
    		return -2;
    	}
    }
    
    function udpate($id = '', $data = array()){
    	if(empty($data) || empty($id)) return -1;
    	if(!empty($data['mypassword']))
    		$data['mypassword'] = md5($data['mypassword']);
    	else 
    		$data['mypassword'] = '';
    	if($this->required($data)) {
    		$this->db->where(array('id' => $id));
			$status = $this->db->update($this->table_name, $data);
			return $status;
    	} else {
    		return -2;
    	}
    }
    
    function delete($id = ''){
		if(empty($id)) return -1;
		$data['deleted'] = 1;
		$this->db->where(array('id' => $id));
		$status = $this->db->update($this->table_name, $data);
		return $status;
    }
    
    function checkUniqueUsername($username = '') {
    	if(empty($username)) return false;
    	$this->db->where('username',$username);
    	$query = $this->db->get($this->table_name);
		$records = $query->result_array();
		if(empty($records)) {
			return true;
		} 
		return false;
    }
    
    function login($username = '', $password = '') {
    	if(empty($username) || empty($password)) {
    		return false;
    	}
    	$mypassword = md5($password);
    	$where = array('username' => $username, 'mypassword' => $mypassword, 'deleted' => 0);
    	$rst = $this->db->get_where($this->table_name, $where);
    	$records = $rst->row_array();
    	if(!empty($records)) {
    		$CI = & get_instance();
    		$CI->load->helper('cookie');
    		$user['id'] = $records['id'];
    		$user['name'] = $records['name'];
    		$user['ltime'] = time();
    		$user['myhash'] = md5($records['username'].$CI->config->item('pwd_hash').$user['ltime']);
    		$CI->session->set_userdata('mylogin', $user);
    		$CI->session->set_userdata('mymsglogin', 'Logged In As ');
    		return true;
    		
    	}
		
    	return false;
    }
    
	function changepwd($id = '', $data = array()){
    	if(empty($data) || empty($id)) return -1;
    	if(!empty($data['mypassword']))
    		$data['mypassword'] = md5($data['mypassword']);
    	else 
    		$data['mypassword'] = '';
    	if(!empty($data['mypassword']) || !empty($data['name'])) {
    		$this->db->where(array('id' => $id));
			$status = $this->db->update($this->table_name, $data);
			return $status;
    	} else {
    		return -2;
    	}
    }
}
?>